package team.bailan.entity.domain;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import team.bailan.common.server.WebSocketServer;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class FractionCalculator {
    int multiple;
    int ticket;
    String roomLevel;
    int roomFactor;
    int maxBeans;

    public void init() {
        multiple = 15;
        roomFactor = RoomLevel.getRoomFactor(roomLevel);
        ticket = RoomLevel.getTicket(roomLevel);
        maxBeans = RoomLevel.getMaxBeans(roomLevel);
    }

    public FractionCalculator(String roomLevel) {
        this.roomLevel = roomLevel;
        init();
    }

    public void redouble() {
        multiple *= 2;
    }

    public void decrease() {
        multiple /= 2;
    }
    public void calculateBeans(String winner, int lordPosition, Integer[] userBeans) {
        int farmerCount = multiple * roomFactor;
        int landlordCount = multiple * roomFactor * 2;
        System.out.println(farmerCount + "-------------------");
        System.out.println(multiple + "------------------");
        if (winner.equals("farmer")) {
            landlordCount += ticket;
            farmerCount -= ticket;
            if (userBeans[lordPosition] - landlordCount >= 0) {
                for (int i = 1; i <= 3; i++) {
                    if (i != lordPosition) {
                        userBeans[i] += farmerCount;
                    }
                }
                userBeans[lordPosition] -= landlordCount;
            } else {
                for (int i = 1; i <= 3; i++) {
                    if (i != lordPosition) {
                        userBeans[i] += userBeans[lordPosition] / 2;
                    }
                }
                userBeans[lordPosition] = 0;
            }

        } else {
            landlordCount = 0;
            farmerCount += ticket;
            for (int i = 1; i <= 3; i++) {
                if (i != lordPosition) {
                    if (userBeans[i] >= farmerCount + ticket) {
                        userBeans[i] -= farmerCount + ticket;
                        landlordCount += farmerCount;
                    } else {
                        landlordCount += userBeans[i];
                        userBeans[i] = 0;
                    }
                }
            }
            userBeans[lordPosition] += landlordCount - ticket;
        }

    }

}
